package com.yuyou.tas.feedback.api;

import com.yuyou.tas.feedback.service.IFeedbackClassService;
import com.yuyou.tas.feedback.service.IFeedbackService;
import com.yuyou.tas.feedback.service.IFeedbackSituationService;
import com.yuyou.tas.service.api.admin.entity.CommJob;
import com.yuyou.tas.service.api.feedback.dto.FeedbackClassPage;
import com.yuyou.tas.service.api.feedback.dto.FeedbackDetailDTO;
import com.yuyou.tas.service.api.feedback.dto.FeedbackListDTO;
import com.yuyou.tas.service.api.feedback.entity.Feedback;
import com.yuyou.tas.service.api.feedback.entity.FeedbackClass;
import com.yuyou.tas.service.api.feedback.entity.FeedbackSituation;
import com.yuyou.tas.service.api.feedback.service.FeedbackAPI;
import com.yuyou.tas.service.api.feedback.vo.req.FeedBackEditReqVo;
import com.yuyou.tas.service.api.feedback.vo.req.FeedbackListVo;
import com.yuyou.tas.service.api.feedback.vo.resp.FeedbackClassesVo;
import com.yuyou.tas.service.api.feedback.vo.resp.FeedbackEditDetail;
import com.yuyou.tas.service.api.feedback.vo.resp.FeedbackNameResp;
import com.yuyou.tas.util.bean.Pagination;
import com.yuyou.tas.util.bean.ResBoolSimpleInfo;
import com.yuyou.tas.util.enums.feedback.FeedbackEnums;
import com.yuyou.tas.util.tool.LogUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by yujin.liu on 2018/11/23.
 */
@RestController
public class FeedbackAPIImpl implements FeedbackAPI{

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
    @Autowired
    private IFeedbackService feedbackService;

    @Autowired
    private IFeedbackSituationService feedbackSituationService;

    @Autowired
    private IFeedbackClassService feedbackClassService;

    @Override
    public int findFeedbackCount(@RequestBody Pagination<FeedbackListDTO> queryPgn) {
    	return feedbackService.findFeedbackCount(queryPgn);
    }

    @Override
    public List<FeedbackListDTO> feedbackList(@RequestBody Pagination<FeedbackListDTO> queryPgn) {
    	return feedbackService.feedbackList(queryPgn);
    }

    @Override
    @Transactional(rollbackFor=Exception.class, timeout=60)
    public FeedbackDetailDTO feedbackDetail(Long feedbackId) {
        Feedback feedback = new Feedback();
        feedback.setId(feedbackId);
        feedback.setIsRead(FeedbackEnums.isRead.read.getValue());
        feedbackService.updateByPrimaryKeySelective(feedback);

        return feedbackSituationService.findSituationByFeedbackId(feedbackId);
    }

    @Override
    public ResBoolSimpleInfo<Feedback> addOrUpdateFeedbackService( @RequestBody FeedBackEditReqVo vo) {   	
    	return feedbackService.addOrUpdateFeedbackService(vo);
    }

    @Override
    public ResBoolSimpleInfo<FeedbackClass> addFeedbackClass(Long classId,  Long userId, Long organId) {
    	return feedbackClassService.addFeedbackClass(classId , userId ,organId);
    }

    @Override
    public ResBoolSimpleInfo<String> batchPublish(Long feedbackClassId) {
    	return feedbackService.batchPublish(feedbackClassId);
    }

    @Override
    public ResBoolSimpleInfo<FeedbackEditDetail> getFeedbackEditDetail(Long feedbackClassId, Long userId, Long organId) {
    	return feedbackClassService.getFeedbackEditDetail(feedbackClassId , userId , organId);
    }

    @Override
    public ResBoolSimpleInfo<FeedbackClassPage> findFeedbackClass(Long classId, Long userId, Long organId, Integer pageIndex, Integer pageSize) {
    	return feedbackClassService.findFeedbackClass(classId , userId ,organId , pageIndex , pageSize);
    }

    @Override
    public ResBoolSimpleInfo<List<FeedbackClassesVo>> findAllClassFeedback(Long teacherId, Long trimesterId) {
        return feedbackClassService.findAllClassFeedback(teacherId , trimesterId);
    }

    @Override
    public List<FeedbackSituation> findAllNeedOperate() {
        return feedbackSituationService.findAllNeedOperate();
    }

    @Override
    public void insert2Job(@RequestBody FeedbackListVo vo) {
    	feedbackService.insert2Job(vo.getFeedbacks() , vo.getFeedbackNotes());
    }

    @Override
    public void AsynUploadJob(@RequestBody CommJob commJob){
    	try {
			feedbackService.AsynUploadJob(commJob);
		} catch (Exception e) {
			LogUtil.error(logger, "AsynUploadJob error",e);
		}
    }

    @Override
    public FeedbackNameResp getFeedbackName(Long feedbackId) {
        return feedbackService.getFeedbackName(feedbackId);
    }

    @Override
    public Feedback selectByPrimaryKey(Long feedbackId) {
        return feedbackService.selectByPrimaryKey(feedbackId);
    }

    @Override
    public void set2Read(@RequestParam("feedbackId")Long feedbackId,@RequestParam("userId") Long userId) {
        feedbackService.set2Read(feedbackId , userId);
    }

}
